package nc.bs.gl.report.impl;

import nc.bs.dao.BaseDAO;
import nc.bs.gl.report.itf.IReportLinkAssDetailService;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ReportLinkAssDetailServiceImpl implements IReportLinkAssDetailService {
    private BaseDAO baseDAO;
    @Override
    public Map<String, Object> handleAssDetailMap(Map<String, Object> paraMap) throws BusinessException {
        List<Object> objs =  (List<Object>) paraMap.get("qryObjs");
        for (Object obj : objs) {
            Map<String,Object> m =  (Map<String,Object>)obj;
            if (m != null && "1001A210000000000RIH".equals(m.get("pk_checktype").toString())) {
                List<String> lists = getXMJuniorByPk(m.get("pk_checkvalue").toString());
                if(lists != null && lists.size()>0) {
                    m.put("pk_checkvalue", String.join(",", lists));
                }
            }
        }

        // 设置默认值 提取到外部方法出 辅助明细表会提示 《请注意》
        paraMap.put("sortType","0");
        paraMap.put("addupByDay", false);
        paraMap.put("disLocalCurySum", false);
        paraMap.put("ckNoAccurNoDsp", false);
        paraMap.put("dspAccumu", false);
        paraMap.put("combineVoucher", false);
        paraMap.put("showBusDate", false);
        paraMap.put("pk_accasoa",new ArrayList<Object>());
        paraMap.put("pagedirection",null);
        paraMap.put("subjVersion","");
        paraMap.put("subjVersionShow",null);
        paraMap.put("pk_currtype","**");
        // 不去掉联查父级项目数据不显示
        paraMap.remove("balanceRangeFrom");
        paraMap.remove("balanceRangeTo");
        paraMap.remove("cbCorpSubjDspBase");
        paraMap.remove("cbQryByCorpAss");
        paraMap.remove("ckNotShowZeroOccurZeroBalance");
        paraMap.remove("ckNotShowZeroOccurZeroBalanceEdit");
        paraMap.remove("ckShowZeroAmount");
        paraMap.remove("ckShowZeroBalance");
        paraMap.remove("class");
        paraMap.remove("currplusacc");
        paraMap.remove("fcrate");
        paraMap.remove("link");
        paraMap.remove("linkxm");
        paraMap.remove("istree");
        paraMap.remove("multliabcenter");
        paraMap.remove("pk_fccurrtype");
        paraMap.remove("qryObjPlusCorp");
        paraMap.remove("rbCorpJun");
        paraMap.remove("showzerobalanceEdit");
        paraMap.remove("showzerobalanceoccur");
        paraMap.remove("showzerooccurEdit");
        paraMap.remove("sumbysubjtype");
        paraMap.remove("twowaybalance");
        return paraMap;
    }

    /**
     * 获取当前项目所有下级
     * @param pk
     * @return
     * @throws BusinessException
     */
    public List<String> getXMJuniorByPk(String pk) throws BusinessException {
        String code = (String)this.getBaseDAO().executeQuery("select code from bd_defdoc where pk_defdoc = '" + pk + "'\n", new ColumnProcessor());
        List<String> lists = (List<String>)this.getBaseDAO().executeQuery(this.buildXMJuniorSQL(code), new ColumnListProcessor());
        return lists;
    }

    /**
     * 构建项目下级查询sql
     * @param code
     * @return
     * @throws BusinessException
     */
    public String buildXMJuniorSQL(String code) throws BusinessException {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT\n" );
        sql.append("   pk_defdoc \n" );
        sql.append(" FROM\n" );
        sql.append("   BD_DEFDOC \n" );
        sql.append(" WHERE\n" );
        sql.append(" 1 = 1 and pid <> '~' and pk_defdoclist = '1001A210000000000RAX' \n" );
        sql.append(" AND code like '"+code+"%'");
        return sql.toString();
    }

    public BaseDAO getBaseDAO() {
        if(this.baseDAO == null) {
            this.baseDAO = new BaseDAO();
        }
        return this.baseDAO;
    }
}
